/**
 * 
 */
package cz.geoserver.KMLpublisher.main;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.webapp.WebAppContext;

/**
 * Class creates instance of Jetty Servlet container and allow to run web-app at localhost 
 * @author mkepka
 *
 */
public class Start {

    private static Server server = new Server();

    /**
     * Method starts servlet container on port 8181 and at context path /publisher
     */
    public static void start(){
        try {
            SocketConnector connector = new SocketConnector();
            connector.setPort(8181);

            server.setConnectors(new Connector[] { connector });
            WebAppContext context = new WebAppContext();
            context.setServer(server);
            context.setContextPath("/publisher");
            context.setWar("src/main/webapp");
            server.addHandler(context);

            server.start();

        } catch (Exception e) {
            if (server != null) {
                try {
                    server.stop();
                } catch (Exception e1) {
                    throw new RuntimeException(e1);
                }
            }
        }
    }

    /**
     * Methods stops servlet container
     * @throws Exception
     */
    public static void stop() throws Exception {
        server.stop();
    }

    /**
     * Main method starts servlet container
     *@param args
     */
    public static void main(String[] args) {
        start();
    }
}